<?php

/*
 * litchicms
 * Copyright (C) 2010-5-1 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
session_start();
define("BRK", strtr(dirname(dirname(__FILE__)), DIRECTORY_SEPARATOR, "/") . "/");
define("SYSTEM", strtr(dirname(__FILE__), DIRECTORY_SEPARATOR, "/") . "/");
define("HTML", BRK . "html/");
define('DEBUG', TRUE);
define("BreakCMSVER", "R324");
define('PHPVER', substr(PHP_VERSION, 0, 1));
set_include_path(SYSTEM . "include/");
if (DEBUG) {
    error_reporting(E_ALL);
    $starttime = microtime(TRUE);
    include 'debug.fun.php';
}else
    error_reporting(0);
$BRK_CONF = $BRK_GLOBAL = $BRK_LANG = $BRK_USER = array();
$BRK_GLOBAL['datearray'] = getdate(time() + 8 * 3600);
/*
  ["seconds"] => int(15)
  ["minutes"] => int(25)
  ["hours"] => int(10)
  ["mday"] => int(28)
  ["wday"] => int(1)
  ["mon"] => int(6)
  ["year"] => int(2010)
  ["yday"] => int(178)
  ["weekday"] => string(6) "Monday"
  ["month"] => string(4) "June"
  [0] => int(1277720715)
 */
empty($_SESSION['time']) && $_SESSION['time'] = $BRK_GLOBAL['datearray'][0];
$BRK_GLOBAL['datearray'][0] - $_SESSION['time'] > 60 && $BRK_GLOBAL['datearray'][0] - $_SESSION['time'] < 3600 && $_SESSION['time'] = $BRK_GLOBAL['datearray'][0];
$BRK_GLOBAL['datearray'][0] - $_SESSION['time'] > 3600 && $_SESSION['brk_login'] = $_SESSION['time'] = null;

//基本配置 基本函数
require 'config.php';
require 'base.fun.php';

is_file(SYSTEM . "cache/site.php") && require SYSTEM . "cache/site.php";
empty($BRK_CONF['prefix']) && exit("prefix");
$BRK_GLOBAL['inajax'] = empty($_GET['ajax']) ? 0 : 1;
$BRK_GLOBAL['ip'] = brk_get_ip();
//GPC
ini_set("magic_quotes_runtime", 0);
if (!(get_magic_quotes_gpc())) {
    $_GET = brk_addslashes($_GET);
    $_POST = brk_addslashes($_POST);
    $_COOKIE = brk_addslashes($_COOKIE);
}
$prelength = strlen($BRK_CONF['prefix']);
foreach ($_COOKIE as $k => $v)
    if (substr($k, 0, $prelength) != $BRK_CONF['prefix'])
        unset($_COOKIE[$k]);
//GPC----end.
//
//
//$BRK_USER=$_SESSION['brk_login'] and rights
key_exists('brk_login', $_SESSION) && $BRK_USER = &$_SESSION['brk_login'];
require_rigths();
//----end
//语言判断  $_COONKIE[$BRK_CONF['prefix'].'lang'] - $_SERVER['HTTP_ACCEPT_LANGUAGE'](需要开启$BRK_CONF['use_http_accept']) - default
//default语言为中文 zh-cn
!is_file(SYSTEM . "language/c." . $BRK_CONF['lang'] . ".php") && $BRK_CONF['lang'] = "zh-cn";
!is_array($BRK_LANG = include SYSTEM . "language/c." . $BRK_CONF['lang'] . ".php") && brk_log(0, "check  c." . $BRK_CONF['lang'] . ".php");

//Alias

if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
    $BRK_GLOBAL['REQUEST_URI_OLD'] = $_SERVER['HTTP_X_REWRITE_URL'];
} else if (isset($_SERVER['HTTP_REQUEST_URI'])) {
    $BRK_GLOBAL['REQUEST_URI_OLD'] = $_SERVER['HTTP_REQUEST_URI'];
} else {
    if (isset($_SERVER['SCRIPT_NAME']))
        $_SERVER['HTTP_REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
    else
        $_SERVER['HTTP_REQUEST_URI'] = $_SERVER['PHP_SELF'];
    if ($_SERVER['QUERY_STRING']) {
        $_SERVER['HTTP_REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
    }
    $BRK_GLOBAL['REQUEST_URI_OLD'] = $_SERVER['HTTP_REQUEST_URI'];
}




$_SERVER['DOCUMENT_ROOT'] = strtr($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR, "/");
$BRK_CONF['rootdir'] = str_replace($_SERVER['DOCUMENT_ROOT'], "", BRK);
$BRK_CONF['rootdir'] = substr($BRK_CONF['rootdir'], 0, strlen($BRK_CONF['rootdir']) - 1);

foreach ($_SERVER as &$v) {
    $v = str_replace($BRK_CONF['rootdir'], "", $v);
}
unset($v);

$BRK_CONF['rootdir'] = strval($BRK_CONF['rootdir']);
//dump($_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
//ksort($_SERVER);
//dump($_SERVER);
//exit;
if ($_SERVER['SCRIPT_NAME'] == "/" . $BRK_CONF['admin_file_name']) {
    if (empty($_SESSION['BASE64'])) {
        $_SESSION['BASE64'] = rand(10, 100);
    } else {
        strpos($_SERVER['QUERY_STRING'], '/') === FALSE && $_SERVER['QUERY_STRING'].='/';
        $temp = substr($_SERVER['QUERY_STRING'], 0, strpos($_SERVER['QUERY_STRING'], '/'));
        $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'], strlen($temp));
        $_SERVER['QUERY_STRING'] = base64_decode($temp) . $_SERVER['QUERY_STRING'];
        strlen($_SERVER['QUERY_STRING']) > 2 && $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'], 2);
        $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
    }
}

$BRK_GLOBAL['alias'] = require SYSTEM . 'data/alias.php';
($_SERVER['REQUEST_URI'] != $_SERVER['SCRIPT_NAME']) && ($_SERVER['REQUEST_URI']{strlen($_SERVER['SCRIPT_NAME'])} != '?') && header301('/index.php');
//dump($_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], 1);
strpos($_SERVER['REQUEST_URI'], '?') === FALSE && $_SERVER['REQUEST_URI'].='/';
strpos($_SERVER['REQUEST_URI'], '/') === FALSE && $_SERVER['REQUEST_URI'].='/';
//dump($_SERVER['REQUEST_URI']);
$alias = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], "/"));
if (key_exists($alias, $BRK_GLOBAL['alias'])) {
    $_SERVER['REQUEST_URI'] = str_ireplace($alias, $BRK_GLOBAL['alias'][$alias], $_SERVER['REQUEST_URI']);
} else {
    $temp = strpos($_SERVER['REQUEST_URI'], "?");
    if ($temp !== FALSE) {
        $alias = substr($alias, 0, $temp);
        $_SERVER['REQUEST_URI'] = str_ireplace($alias . '?', $BRK_GLOBAL['alias'][$alias] . '/', $_SERVER['REQUEST_URI']);
    }
}
if (strpos(substr($_SERVER['REQUEST_URI'], 0, 12), "site-admin") !== FALSE) {
    define('SITE', 'admin');
} else {
    define('SITE', '');
}


//前台站点
if (SITE == '') {
    $RT = array('v' => array());


    $_SERVER['REQUEST_URI'] = strtr($_SERVER['REQUEST_URI'], "/", '-');
    $rt = explode("-", $_SERVER['REQUEST_URI']);
    $rt = array_filter($rt);
    $rt = array_merge($rt);

    $count = count($rt);
    $count % 2 == 1 && $count-=1;
    $count = $count / 2;
    for ($i = 0; $i < $count; $i++) {
        $RT['v'][$rt[$i * 2]] = $rt[$i * 2 + 1];
    }
    empty($RT['v']['mode']) && $RT['v']['mode'] = '';
    empty($RT['v']['action']) && $RT['v']['action'] = '';
    krsort($RT['v']);
    $GET = &$RT['v'];
    $_GET = &$RT['v'];

    if ($BRK_SET['is_building'] == 'yes') {
        define('IS_BUILD_TPL', TRUE);
        $BRK_SET['cachetime'] = 1;
        $BRK_SET['htmltime'] = 1;
    }

    define("TPL_DIR", $BRK_CONF['tpldir'] . '/' . $BRK_CONF[$RT['v']['mode'] . 'tpl'] . '/');
    define("TPL_CACHE_DIR", SYSTEM . "cache/tpl_" . $BRK_CONF[$RT['v']['mode'] . 'tpl'] . '/');


    empty($RT['v']['m']) && $RT['v']['m'] = $BRK_SET['defualt_channel'];
    empty($RT['v']['part']) && $RT['v']['part'] = $RT['v']['m'];

    /*
     * 根据优先级 从上至下 选择模板
     * 10  action (回复,评论)
     * 20  id   (文章id)
     * 30  category  (文章分类)
     * 40  part  (数据集) Unavailable
     * 50  m  (频道,模块)
     * 60  theme (主题)  Unavailable
     *
     * 模板类型
     * tpl 全局
     * listtpl 列表
     * viewtpl 详细内容
     *
     * 允许出现的组合
     * 1 m-xxx
     * 2 m-xxx-part-xxx
     * 3 m-xxx-part-xxx-id-xxx
     * 4 m-xxx-category-xxx
     * 5 m-xxx-category-xxx-id-xxx
     * 6
     * 7
     * 特殊的 Unavailable
     */
//    dump($BRK_SET['cachemode']);exit;
    if (!empty($RT['v']['nocache'])) {
        $BRK_SET['cachemode'] = "nocache";
    }
    if (!empty($RT['v']['attach'])) {
        $BRK_SET['cachemode'] = "attachment";
    }
    require 'cachemode/' . $BRK_SET['cachemode'] . '.inc.php';


    //引入配置缓存
    //是否重新编译模板?
//1    //公共信息 (频道列表,全局输出,友情连接等)  (缓存到文件)
//去首页,!=0则判断m是否存在,不存在则去首页,存在则去m里判断action和其他参数
//$BRK_CONF['ms'] 写在conf.cache.php里面
    //$BRK_CONF['ms']=array('news','products');
//组合模板路径
//详细
//举例
    /*
      require 'sql.class.php';
      require 'lr.class.php';
      require 'template.fun.php';
      $Db=new sql();
      $Db->connect("localhost","tiefa","xnzeve","brk_", "brkcms");
      $category=new category(1,'news');
      $category->loadsub();
      dump($category->sub);
     */

    //$tplstr=file_get_contents(BRK."theme/".$BRK_CONF['tpl']."/index.php");
    //

    $debug = array(
        "时间消耗:" . (microtime(TRUE) - $starttime) . "s",
        "内存消耗:" . (memory_get_usage() / 1024 / 1024) . "mb",
    );
} else {
    require SYSTEM . 'litchi/index.php';
}

debug_html($debug);
//2    //不需要公共信息
//路由分发END
//访问日志 (日志,IP,路由,请求数据,缓存/更新)
?>
